/*
 * Copyright (c) 2011 Nokia Corporation.
 */

#ifndef LEVELCONTROLLER_HPP
#define LEVELCONTROLLER_HPP

#include <QObject>
#include <QStringList>
#include <QHash>
#include <QVariant>

#if defined(IAP_ENABLED)
#include <iapclient.h>
#endif

class LevelModel;
class Level;

/***************************************************************************************************
*Class used for handling IAP(in application purchasing) and level/theme loading.                   *
***************************************************************************************************/
class LevelController : public QObject
{
    Q_OBJECT
    /***********************************************************************************************
    *A read-only property used to control the game characters look.                                *
    ***********************************************************************************************/
    Q_PROPERTY(QString face READ getFace NOTIFY faceChanged)
public:
    /***********************************************************************************************
    *The constructor, requiring a mandatory level object.                                          *
    ***********************************************************************************************/
    explicit LevelController(Level *level,QObject *parent = 0);

    /***********************************************************************************************
    *An invokable method used to trigger level/theme loading form qml.                             *
    ***********************************************************************************************/
    Q_INVOKABLE void load(int index);
    /***********************************************************************************************
    *An invokable method used to trigger IAP purchase form qml.                                    *
    ***********************************************************************************************/
    Q_INVOKABLE void buy(int index);

    /***********************************************************************************************
    *A method used to access the model used to represent purchasable items.                        *
    ***********************************************************************************************/
    LevelModel* getLevelModel() const;

    /***********************************************************************************************
    *A method returning the current value of the face property.                                    *
    ***********************************************************************************************/
    QString getFace() const;

private slots:
#if defined(IAP_ENABLED)
    /***************************************************************************************************
    *IAP data received handling slots.                                                                 *
    ***************************************************************************************************/
    /***********************************************************************************************
    *A slot triggered by the IAPClients productDataReceved signal.                                 *
    ***********************************************************************************************/
    void productDataReceved(int requestId,QString status,IAPClient::ProductData data);
    /***********************************************************************************************
    *A slot triggered by the IAPClients restorableProductsReceived signal.                         *
    ***********************************************************************************************/
    void restorableProductsReceived(int requestId,QString status,IAPClient::ProductList data);
    /***********************************************************************************************
    *A slot triggered by the IAPClients userAndDeviceDataReceived signal.                          *
    ***********************************************************************************************/
    void userAndDeviceDataReceived(int requestId,QString account,QString imei,QString imsi,
                                   QString country,QString language,QString deviceModel);
    /**************************************************************************************************/

    /***************************************************************************************************
    *IAP purchase flow handling slots.                                                                 *
    ***************************************************************************************************/
    /***********************************************************************************************
    *A slot triggered by the IAPClients purchaseCompleted signal.                                  *
    ***********************************************************************************************/
    void purchaseCompleted(int requsetId,QString status,QString ticket);
    /***********************************************************************************************
    *A slot triggered by the IAPClients purchaseFlowFinished signal.                               *
    ***********************************************************************************************/
    void purchaseFlowFinished(int requsetId);
    /**************************************************************************************************/

    /***************************************************************************************************
*IAP restoration flow handling slots.                                                              *
***************************************************************************************************/
    /***********************************************************************************************
    *A slot triggered by the IAPClients restorationCompleted signal.                               *
    ***********************************************************************************************/
    void restorationCompleted(int requsetId,QString status,QString ticket);
    /***********************************************************************************************
    *A slot triggered by the IAPClients restorationFlowFinished signal.                            *
    ***********************************************************************************************/
    void restorationFlowFinished(int requsetId);
    /**************************************************************************************************/
#endif
signals:
    /***********************************************************************************************
    *A signal emitted after the purchase/restore has been finished, to hide the software keys.     *
    ***********************************************************************************************/
    void hack();

    /***********************************************************************************************
    *A signal emitted after successfully loading a new level, used to trigger qml level reload.    *
    ***********************************************************************************************/
    void levelReload();
    /***********************************************************************************************
    *A signal emitted after loading a theme, notifies that the face properties value has changed.  *
    ***********************************************************************************************/
    void faceChanged();

private:
    /***********************************************************************************************
    *A private method used to trigger purchasable item loading.                                    *
    ***********************************************************************************************/
    void loadItems();
    /***********************************************************************************************
    *A private method used request IAP data for a single item.                                     *
    ***********************************************************************************************/
    void requestItemData();
    /***********************************************************************************************
    *A private method used unlock purchasable items.                                               *
    ***********************************************************************************************/
    void unlock(QString itemId,QString purchaseTicket);

    /***********************************************************************************************
    *The level model object.                                                                       *
    ***********************************************************************************************/
    LevelModel *m_Model;
    /***********************************************************************************************
    *The IAPClient object.                                                                         *
    ***********************************************************************************************/
#if defined(IAP_ENABLED)
    IAPClient *m_IAPClient;
#endif
    /***********************************************************************************************
    *The level object.                                                                             *
    ***********************************************************************************************/
    Level *m_level;

    /***********************************************************************************************
    *A list used to hold the strings defining items loaded from the Items.data file.               *                                                                            *
    ***********************************************************************************************/
    QStringList itemLines;
    /***********************************************************************************************
    *A request queue for the level items, int request id, QString product id QString level name.   *
    ***********************************************************************************************/
    QHash<int,QPair<QString,QString> > levelRequests;
    /***********************************************************************************************
    *A request queue for theme items, int request id, QString product id QString theme name.       *
    ***********************************************************************************************/
    QHash<int,QPair<QString,QString> > themeRequests;
    /***********************************************************************************************
    *A string holding the value of the face property.                                              *
    ***********************************************************************************************/
    QString face;
};

#endif // LEVELCONTROLLER_HPP
